package cn.edu.scau.dropship.controller;

import cn.edu.scau.dropship.entity.Parameter;
import cn.edu.scau.dropship.entity.ParameterExample;
import cn.edu.scau.dropship.ServiceImpl.ParameterServiceImpl;
import cn.edu.scau.dropship.service.ParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/parameter")
public class ParameterController {

    @Autowired
    private ParameterService parameterService;

    /*
     * 参数新增，管理员
     * */
    @PostMapping(value = "/add")
    @ResponseBody
    public String insert(HttpServletRequest request) {
        //获取前台传值
        String paramPk=request.getParameter("pk");
        String paramValue=request.getParameter("pv");
        String paramDescription=request.getParameter("pd");
        //传值判断
        if(paramPk.equals("")||paramValue.equals("")||paramDescription.equals("")){
            return "所有字段不允许为空，请重新输入！";
        }
        //插入
        Parameter parameter = new Parameter();
        parameter.setParamPk(paramPk);
        parameter.setParamValue(Double.valueOf(paramValue));
        parameter.setParamDescription(paramDescription);
        int result = parameterService.insert(parameter);
        if (result > 0)
            return "插入成功";
        return "插入失败";
    }

    /*
     * 参数删除，管理员
     * */
    @GetMapping(value = "/delete")
    @ResponseBody
    public String deleteById(HttpServletRequest request) {
        Integer id=Integer.valueOf(request.getParameter("id"));
        int result = parameterService.deleteByPrimaryKey(id);
        if (result > 0)
            return "删除成功";
        return "删除失败";
    }

    /*
     * 参数修改，管理员
     * */
    @PostMapping(value = "/update")
    @ResponseBody
    public String updateById(HttpServletRequest request) {
        //获取前台传值
        Integer id=Integer.valueOf(request.getParameter("id"));
        String paramPk=request.getParameter("pk");
        String paramValue=request.getParameter("pv");
        String paramDescription=request.getParameter("pd");
        //传值判断
        if(paramPk.equals("")||paramValue.equals("")||paramDescription.equals("")){
            return "所有字段不允许为空，请重新输入！";
        }
        //插入
        Parameter parameter = new Parameter();
        parameter.setId(id);
        parameter.setParamPk(paramPk);
        parameter.setParamValue(Double.valueOf(paramValue));
        parameter.setParamDescription(paramDescription);
        int result = parameterService.updateByPrimaryKey(parameter);
        if (result > 0)
            return "修改成功";
        return "修改失败";
    }

    @GetMapping(value = "/selectById{id}")
    public Parameter selectById(@RequestParam("id") Integer id) {
        Parameter parameter = parameterService.selectByPrimaryKey(id);
        return parameter;
    }

    /*
     * 参数列表查询，管理员
     * */
    @GetMapping(value = "/selectAll")
    public List<Parameter> selectAll() {
        ParameterExample parameterExample = new ParameterExample();
        ParameterExample.Criteria criteria = parameterExample.createCriteria();
        criteria.andIdGreaterThan(0);
        List<Parameter> list = parameterService.selectByExample(parameterExample);
        return list;
    }

    /*
     * 参数列表查询，管理员
     * */
    @PostMapping(value = "/selectByPk")
    @ResponseBody
    public List<Parameter> selectByPk(HttpServletRequest request) {
        String pk=request.getParameter("pk");
        ParameterExample parameterExample = new ParameterExample();
        ParameterExample.Criteria criteria = parameterExample.createCriteria();
        criteria.andParamPkEqualTo(pk);
        List<Parameter> list = parameterService.selectByExample(parameterExample);
        return list;
    }

    //获取参数表列表
    @RequestMapping(value = "/getParList",method = RequestMethod.POST)
    @ResponseBody
    public List<Parameter> getParList(HttpServletResponse response){
        List<Parameter> list=parameterService.getlist();
        return list;
    }
}
